package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"wsyxapi/client"
	"wsyxapi/internal/svc"
	"wsyxapi/internal/types"
	"wsyxapi/utils/ali"

	"github.com/zeromicro/go-zero/core/logx"
)

type AliNotifyLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAliNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliNotifyLogic {
	return &AliNotifyLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AliNotifyLogic) AliNotify(req *types.AliNotifRequest, writer http.ResponseWriter, request *http.Request) (resp *types.AliNotifResponse, err error) {
	// todo: add your logic here and delete this line
	fmt.Println("************req")
	str, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	fmt.Println(string(str))
	notifDate, err := ali.Notify(writer, request)
	fmt.Println(notifDate, err)
	if err != nil {
		return nil, err
	}

	fmt.Println(notifDate.OutTradeNo)

	client.OrderNotify(context.Background(), notifDate.OutTradeNo)
	//TODO:修改订单状态为已支付 notifDate.OutTradeNo

	//TODO:扣减商品库存

	//TODO：待补充...
	return
}
